package receivers;

import com.example.maze.ConnectionInfo;
import com.example.maze.LobbyActivity;
import com.example.maze.MainActivity;
import com.example.maze.MyLog;
import com.example.maze.MyProgressDialog;
import com.example.maze.R;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import services.LoginService;
import tw.edu.tku.im.maze.LoginRep;
import tw.edu.tku.im.maze.RepPacket;
import android.app.Activity;

public class LoginRepReceiver extends BroadcastReceiver {
   //MyProgressDialog mProgressDialog;
   Activity currentUIActivity;
   //public LoginRepReceiver(MyProgressDialog dialog) {
   //   mProgressDialog= dialog;
   //}
   public LoginRepReceiver(Activity activity) {
      this.currentUIActivity= activity; 
   }
   
   // -------------------------------------------------- 
   //[ 前往 LobbyActivity
   private void goToNextActivity(Context context) {
      Intent intent= new Intent(context, LobbyActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(intent);
   }
   
   @Override
   public void onReceive(Context context, Intent intent) {
      ((MainActivity)currentUIActivity).dismissMyDialog();
      int status= intent.getIntExtra(LoginService.STATUS, -1); // -1: status not found
      
      switch(status) {
      case LoginService.LOGIN_REP_STATUS_SUCCESS: 
         ConnectionInfo.parseAndSaveInfo((RepPacket)intent.getParcelableExtra(LoginService.PACKET));
         goToNextActivity(context);
         break;
      case LoginService.LOGIN_REP_STATUS_ERROR_ID: 
         ((MainActivity)currentUIActivity).showMsgDialog(R.string.dialog_id_error_ch);
         break;
      case LoginService.LOGIN_REP_STATUS_ERROR_PWD:             
         ((MainActivity)currentUIActivity).showMsgDialog(R.string.dialog_pwd_error_ch);
         break;
      default: 
         ((MainActivity)currentUIActivity).showMsgDialog(R.string.a1_connectionTimeout_ch);
      }
   }
   // -------------------------------------------------- 
}
